/* 
 * Copyright 2009 HIT-CIR (HIT Center for Information Retrieval). 
 * 
 * author: chuter
 * mail:   lliu@ir.hit.edu.cn
 */
package nlpeap.toolrepository;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import edu.hit.irlab.nlp.irlas.IRLAS;
import edu.hit.irlab.nlp.ner.NER4j;
import edu.hit.irlab.nlp.parser.Parser;

/**
 * The <code>tool</code> Repository is a registry of all NLP basic tools
 * or other tools.
 * 
 * @author chuter
 */

public class ToolRepository {
	//the map a tool and it's name
	private HashMap<String, Tool> toolsmap;
	private static ToolRepository _tool_repository = null;
	
	public static ToolRepository getInstance() {
		if (_tool_repository == null) {
			_tool_repository = new ToolRepository();
			try {
				_tool_repository.installdefaultools();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return _tool_repository;
	}
	
	private  ToolRepository() { toolsmap = new HashMap<String, Tool>(); }
	
	/**
	 * Install default tools, including irlas, ner and parser,
	 * they all come from <a href="http://ir.hit.edu.cn">HIT-CIR</a>
	 * @throws IOException 
	 */
	public void installdefaultools() throws IOException {
		IRLAS irlas = new IRLAS();
		NER4j ne = new NER4j();
		
		installtool(irlas, Tool.WordSegment);
		installtool(ne, Tool.NER);
	}
	
	/**
	 * Install one tool to the repository.
	 * 
	 * @param newtool
	 * 		the tool want to register to this repository
	 * @param toolName
	 * 		the name of the tool
	 */
	public void installtool(Tool newtool, String toolName) {
		if (toolsmap.containsKey(toolName)) {
			System.err.println("there is aready a tool named " + toolName);
			return;
		}
		try {
			newtool.loadResource();
			toolsmap.put(toolName, newtool);
		} catch (LoadException e) {
			System.err.println(toolName+": fetal error when loading the resource");
		}
	}
	
	/** Uninstall all the tools of the repository */
	@SuppressWarnings("unchecked")
	public void uninstallalltools() {
		Iterator<Entry<String, Tool>> it = toolsmap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry m=(Map.Entry)it.next();
			toolsmap.get(m.getKey()).releaseResource();
		}
		toolsmap.clear();
	}
	
	/**
	 * Uninstall a specific tool in the repository
	 * @param toolname
	 * 		the name of the tool
	 */
	public void uninstalltool(String toolname) {
		if (toolsmap.containsKey(toolname)) { 
			toolsmap.get(toolname).releaseResource();
			toolsmap.remove(toolname);
		}
	}
	
	/**
	 * Check wether the tool with the name has been installed
	 * @param toolname
	 * 		the name of the tool
	 * @return
	 */
	public boolean hastool(String toolname) {
		if (toolsmap.containsKey(toolname))
			return true;
		return false;
	}
	
	public Tool gettool(String toolname) {
		if (!hastool(toolname))
			return null;
		return toolsmap.get(toolname);
	}
	
	public int size() { return toolsmap.size(); }
}